package com.jwb.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@Slf4j
public class ProtocolUtil {

	/**
	 * 头条图片上传并获得urllist
	 * @param imgList
	 * @param cookies
	 * @return
	 */
	public static JSONObject getNewHtmlImgUrlForTT(List<String> imgList,String cookies)
	{
		JSONObject imgNewArray = new JSONObject();
		
		for(String img:imgList){		
		   	String  postUrl="https://mp.toutiao.com/mp/agw/article_material/photo/upload_picture?type=ueditor&pgc_watermark=1&action=uploadimage&encode=utf-8";
	    	Map<String, String> headers = new HashMap<String, String>();
	    	JSONObject paraData = new JSONObject();
	    	paraData.put("name", "upfile");
			headers.put("Host", "mp.toutiao.com");
			//headers.put("Origin", "mp.toutiao.com");
			headers.put("Cookie", cookies);
	    	try {
				URI uri =new URI(img);
				JSONObject entityjson=ToolsUtil.uploadNetImgFile(uri,postUrl,headers,paraData);
				log.info("getNewHtmlImgUrl(): "+entityjson);
				if(entityjson!=null&&entityjson.getInteger("code")==0){
					imgNewArray.put(img, entityjson.getString("url"));
				}else{
					imgNewArray.put(img, "");
				}
			} catch (Exception e) {
				e.printStackTrace();
				
			}
		}
		
		return 	imgNewArray;
	}
	
	/**
	 * 头条封面上传获取元素
	 * @param imgUrl
	 * @param cookies
	 * @return
	 */
	public static JSONArray getConverImgUrlForTT(String imgUrl,String cookies)
	{
		JSONArray gc_feed_covers = new JSONArray();			
	   	String postUrl="https://mp.toutiao.com/mp/agw/article_material/photo/upload_picture?type=ueditor&pgc_watermark=1&action=uploadimage&encode=utf-8";    
    	Map<String, String> headers = new HashMap<String, String>();
    	JSONObject paraData = new JSONObject();
    	paraData.put("name", "upfile");
		headers.put("Host", "mp.toutiao.com");
//		headers.put("Origin", "mp.toutiao.com");
		headers.put("Cookie", cookies);
    	try {
    		 JSONObject info = new JSONObject();
			 URI uri =new URI(imgUrl);
			 info=ToolsUtil.uploadNetImgFile(uri,postUrl,headers,paraData);
			 if(info!=null&&info.getInteger("code")==0){
				 
				
				 JSONObject jso = new JSONObject();
				 jso.put("id", "");
				 jso.put("url", info.getString("web_url"));
				 jso.put("uri", info.getString("web_uri"));
				 jso.put("ic_uri", "");
				 jso.put("thumb_width", info.getInteger("width"));
				 jso.put("thumb_height", info.getInteger("height"));
				 gc_feed_covers.add(jso);
				 
			 }else{
				 gc_feed_covers=null;
			 }
			 
		} catch (Exception e) {
			e.printStackTrace();
			gc_feed_covers = null;
		}	
		
		return 	gc_feed_covers;
	}
		
		
	
	
	
	
	/**
	 * 头条视频上传
	 * jsoReq: {"filePath":"","videoCoverCount":11,"size":500000}
	 */
	public  static JSONObject  uploadVideoFileForTT(String cookies,JSONObject jsoReq)
	{
		
		JSONObject jsoResp= new JSONObject();
		JSONObject fileInfo = new JSONObject();   // {"name":"","path":"",ext:"","size":"","crc":""}
	    String filePath=jsoReq.getString("filePath");
	    File file = new File(filePath);
		if(file.exists()&&file.isFile()){
			try{
				String name = file.getName();
				fileInfo.put("name", name);
				fileInfo.put("path", filePath);
				fileInfo.put("ext", name.indexOf(".")>-1?name.split("\\.")[1]:"");
				fileInfo.put("size", String.valueOf(file.length()));
				if(jsoReq.getInteger("size")==0){
					fileInfo.put("crc", ToolsUtil.getFileCRCCode(file));
				}
			}catch(Exception e){
				e.printStackTrace();
				jsoResp.put("respCode","-1");
				jsoResp.put("respDesc", "获取"+filePath+" 文件信息异常");
				jsoResp.put("respMsg", "获取"+filePath+" 文件信息异常");
				return jsoResp;
			}
		}else{
			jsoResp.put("respCode","-1");
			jsoResp.put("respDesc", filePath+" 文件不存在");
			jsoResp.put("respMsg", filePath+" 文件不存在");
			return jsoResp;
		}
	    
	    int videoCoverCount=jsoReq.getInteger("videoCoverCount");      //封面次数
	    String authorization="";
	    String x_tt_access="";
	    String content="";
	    String fileSize=fileInfo.getString("size"); // "4925317"
	    
	  //  String cookies="passport_csrf_token=6921d8ce370295d4f9d68bfff840e202;s_v_web_id=verify_k8qoaovq_f164B7VF_YiqQ_4WIN_BH8Z_07Dsfga5wKNL;sso_auth_status=c73d552366efd68ef87dfcfb2b7d51e9;sso_uid_tt=998a1307e1ec8ca7329b0e53f8ef3180;sso_uid_tt_ss=998a1307e1ec8ca7329b0e53f8ef3180;toutiao_sso_user=6aba4914ccd86eb6595ff0550de8a8f2;toutiao_sso_user_ss=6aba4914ccd86eb6595ff0550de8a8f2;passport_auth_status=74cd2aa025ce22a57f0e35605f4958d8%2C219ae1dfd2ee07ed455cd0752c9616a4;sid_guard=9bef42d870d744aa76169b5dedeb45ed%7C1586310501%7C5184000%7CSun%2C+07-Jun-2020+01%3A48%3A21+GMT;uid_tt=8f71874e3f3ca9a188fc86537f89b8c9;uid_tt_ss=8f71874e3f3ca9a188fc86537f89b8c9;sid_tt=9bef42d870d744aa76169b5dedeb45ed;sessionid=9bef42d870d744aa76169b5dedeb45ed;sessionid_ss=9bef42d870d744aa76169b5dedeb45ed;gftoken=OWJlZjQyZDg3MHwxNTg2MzEwNTAxNjh8fDAGBgYGBgY;SLARDAR_WEB_ID=9bd57122-94ce-4497-9675-52f309eef873;tt_webid=6813151764553516552";	
	 //	cookies="_ga=GA1.2.24301998.1584663458; install_id=0; sid_tt=d0a9a08842fb7d25b12fbea597240f86; sid_guard=d0a9a08842fb7d25b12fbea597240f86; sessionid=d0a9a08842fb7d25b12fbea597240f86; tt_webid=6810540743864059400; uid_tt=7db44c10f675780eb908c389f7c043e22dd149dff269cd4582a57c9ad6ee0e68; gftoken=ZDBhOWEwODg0MnwxNTg1NzA2NjcyNjN8fDAGBgYGBgY; SLARDAR_WEB_ID=1c7e18ec-ad55-4b57-9803-3e32fc17c539; passport_csrf_token=c4c13338cb91b256b557a5899a636b2e; sso_uid_tt=44388a0206e4f50fcab45d3ed3b0ea75; sso_uid_tt_ss=44388a0206e4f50fcab45d3ed3b0ea75; toutiao_sso_user=c95f7b9c8e7cf2008f7e914a0d3f6756; toutiao_sso_user_ss=c95f7b9c8e7cf2008f7e914a0d3f6756; s_v_web_id=k8r1steo_hNzQboyc_ASbU_4RuG_AGAp_aY9Bsqq6v3Zy";	    
	    String url = "https://mp.toutiao.com/pgc/mp/core/article/upload_token?user=pgc";						
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("referer", "https://mp.toutiao.com/profile_v3/graphic/publish");   
		headers.put("Host", "mp.toutiao.com");
//		headers.put("Origin", "mp.toutiao.com");
		headers.put("Cookie", cookies);	 			
        JSONObject jso =ToolsUtil.getHttps(url,headers) ;
        System.out.println("jso-upload_token(获取上传空间服务器密钥):"+jso);	 
        if(jso.getInteger("code")!=0){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-upload_token 获取上传空间服务器密钥失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        
        authorization=jso.getString("data");
        try
        {
        	x_tt_access=authorization.split(":")[3];
        }
        catch(Exception e)
        {
        	x_tt_access="cfc067bb39feff592af82085b42e6dc3";
        	e.printStackTrace();
        }
	    
	    
        headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/json");
		headers.put("authorization", authorization); 	
		headers.put("x-tt-access", x_tt_access); 			
        url = "https://vas.snssdk.com/video/openapi/v1/?action=GetVideoUploadParams&s=6m6u6iw2r04&use_edge_node=1&file_size="+fileSize;						
        jso =ToolsUtil.getHttps(url,headers) ;
        System.out.println("jso-openapi/v1(获取上传空间服务器许可证):"+jso);
        if(jso.getInteger("code")!=2000){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-openapi/v1 获取上传空间服务器许可证失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        String oid=jso.getJSONObject("data").getString("oid");
        String tos_sign=jso.getJSONObject("data").getString("tos_sign");
        String tos_host=jso.getJSONObject("data").getString("tos_host");
        String vid=jso.getJSONObject("data").getString("vid");
        String token=jso.getJSONObject("data").getString("token");
        System.out.println("vid----"+vid);
        System.out.println("token----"+token);


        headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary8wFdKGjSgjw3wIfx");
		headers.put("authorization", tos_sign); 			
		headers.put("Host", tos_host);		 
	    url = "https://"+tos_host+"/"+oid+"?uploads";
	    content="------WebKitFormBoundary8wFdKGjSgjw3wIfx--";
	    jso =ToolsUtil.postHttps(url,content,headers) ;	  
	    if(jso.getInteger("success")!=0){
	    	jsoResp.put("respCode", "-1");
	    	jsoResp.put("respDesc", "jso-uploads 文件生成上传id失败");
	    	jsoResp.put("respMsg", jso.toString());
	    	return jsoResp;
	    }
	    System.out.println("jso-uploads(文件服务器生成上传id):"+jso);
	    String uploadID=jso.getJSONObject("payload").getString("uploadID");
	    System.out.println("uploadID:"+uploadID);
	    
	    
	    
	    int eachSize = Integer.valueOf(jsoReq.containsKey("size")?jsoReq.getInteger("size"):500000); 
	    boolean valid = true;
	    JSONArray fileInfos = new JSONArray();
	    if(eachSize==0){
	    	fileInfos.add(fileInfo);
	    }else{
	    	try{
		    	String name = fileInfo.getString("name");
		    	String ext = fileInfo.getString("ext");
		    	String root = filePath.substring(0, filePath.lastIndexOf(name));
		    	String prefix ="";
		    	if("".equals(ext))
		    	{
		    		prefix=name;
		    		
		    	}
		    	else
		    	{
		    		prefix = name.substring(0,name.lastIndexOf("."+ext));
		    	}
		    	
				String joint = "_";
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
				byte[] bytes = new byte[eachSize];
				int length = -1;
				int idx = 1;
				while((length =bis.read(bytes))>-1){ 
					JSONObject info = new JSONObject(); 
					String fpath =root+prefix+joint+idx+"."+ext;
					info.put("path", fpath);
					info.put("ext", ext);
					if("".equals(ext))
					{
						info.put("name", prefix+joint+idx);
					}
					else
					{
						info.put("name", prefix+joint+idx+"."+ext);
					}
					try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fpath))){
						bos.write(bytes,0,length);
						idx++;
						info.put("size", length);
					}catch (Exception e) {
						e.printStackTrace();
						valid=false;
						break;
					}		
					info.put("crc", ToolsUtil.getFileCRCCode(new File(fpath)));
					fileInfos.add(info);
				}
				bis.close();
		    	if(!valid){
		    		jsoResp.put("respCode", "-1");
		    		jsoResp.put("respDesc", "jso-uploads 分片拆分("+prefix+joint+idx+")"+" 失败");
		    		jsoResp.put("respMsg", "jso-uploads 分片拆分("+prefix+joint+idx+")"+" 失败");
		    		return jsoResp;
		    	}	
	    	
	    	}catch(Exception e){
	    		jsoResp.put("respCode", "-1");
	    		jsoResp.put("respDesc", "jso-uploads 分片拆分异常");
	    		jsoResp.put("respMsg", "jso-uploads 分片拆分-开关流失败");
	    		return jsoResp;
	    	}	    	
	    }	    
	    
	    content ="";
	    for(int i=0;i<fileInfos.size();i++){
	    	int partNumber =i+1;
	    	JSONObject fi = fileInfos.getJSONObject(i);
	    	url = "https://"+tos_host+"/"+oid+"?partNumber="+partNumber+"&uploadID="+uploadID;		
	    	byte[] stream;	
		    stream=ToolsUtil.fileConvertToByteArray(new File(fi.getString("path")));
		    JSONObject paraData = new JSONObject();
	    	paraData.put("url", url);
	    	paraData.put("method", "PUT");
	    	paraData.put("stream", stream);			   
		    headers = new HashMap<String, String>();
			headers.put("Accept", "*/*"); 
			headers.put("Content-Type", "application/octet-stream");
			headers.put("authorization", tos_sign); 			
			headers.put("Host", tos_host);		 
			headers.put("Content-CRC32", fi.getString("crc"));			
			headers.put("Content-Length", stream.length+"");
	    	jso = ToolsUtil.uploadStream(paraData,headers);
	    	if(!jso.getString("respCode").equals("0")){
	    		valid = false;
	    		jsoResp.put("respCode", "-1");
	    		jsoResp.put("respDesc", "jso-uploads 分片上传("+partNumber+")_"+uploadID+"失败");
	    		jsoResp.put("respMsg", jso.getString("respDesc"));
	    		break;
	    	}
	    	content+= partNumber+":"+fi.getString("crc");
	    	if(i!=fileInfos.size()-1){
	    		content+=",";
	    	}
		    System.out.println("jso-uploads(分片上传("+partNumber+")_"+uploadID+"):"+jso);	    	
	    }
	    
	    if(!valid){
	    	return jsoResp;
	    }
	    
	    
	    
	    //循环分片
	    
	    
//	    int partNumber=1;		
//	    
//	    url = "https://"+tos_host+"/"+oid+"?partNumber="+partNumber+"&uploadID="+uploadID;			  
//	    filePath="D:\\video\\test.mp4";
//	    File file =new File(filePath);
//	    String fileCrc32= ToolsUtil.getFileCRCCode(new File(filePath));
//	    byte[] stream;	
//	    stream=ToolsUtil.fileConvertToByteArray(file);		 
//	    JSONObject paraData = new JSONObject();
//    	paraData.put("url", url);
//    	paraData.put("method", "PUT");
//    	paraData.put("stream", stream);			   
//	    headers = new HashMap<String, String>();
//		headers.put("Accept", "*/*"); 
//		headers.put("Content-Type", "application/octet-stream");
//		headers.put("authorization", tos_sign); 			
//		headers.put("Host", tos_host);		 
//		headers.put("Content-CRC32", fileCrc32);			
//		headers.put("Content-Length", stream.length+"");
//    	jso = ToolsUtil.uploadStream(paraData,headers);
//    	if(!jso.getString("respCode").equals("0")){
//    		jsoResp.put("respCode", "-1");
//    		jsoResp.put("respDesc", "jso-uploads 分片上传("+partNumber+")_"+uploadID+"失败");
//    		jsoResp.put("respMsg", jso.getString("respDesc"));
//    		return jsoResp;
//    	}
//	    System.out.println("jso-uploads(分片上传("+partNumber+")_"+uploadID+"):"+jso);
//	    
	    
	    
	    //组合分片
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "text/plain;charset=UTF-8");
		headers.put("authorization", tos_sign); 			
		headers.put("Host", tos_host);		 
		url = "https://"+tos_host+"/"+oid+"?&uploadID="+uploadID;	
		//1:f82ffeb5,2:eaabdcf1
	    jso =ToolsUtil.postHttps(url,content,headers) ;
	    if(jso.getInteger("success")!=0){
	    	jsoResp.put("respCode", "-1");
	    	jsoResp.put("respDesc", "jso-uploads 文件上传确认_"+uploadID+" 失败");
	    	jsoResp.put("respMsg", jso.toString());
	    	return jsoResp;
	    }
	    System.out.println("jso-uploads(文件上传确认_"+uploadID+"):"+jso);
	    

	    
	    url="https://vas.snssdk.com/video/openapi/v1/?action=UpdateVideoUploadInfos&vidc=lf&vts=1586420571714421877&host="+tos_host+"&region=CN&province=12&edge_node=lf&upload_mode=serial&file_size="+fileSize+".000000&strategy=long_memory_filter_v2&user_ip=60.29.142.82";
	    headers = new HashMap<String, String>();		  
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", "application/json");
		headers.put("authorization", authorization); 	
		headers.put("x-tt-access", x_tt_access); 			
		JSONObject paraData = new JSONObject();
    	paraData.put("vid", vid);
    	paraData.put("oid", oid);
    	paraData.put("token", token);		
    	paraData.put("poster_ss", 0);	
    	paraData.put("is_exact_poster", false);	
    	paraData.put("user_reference", "");			 
		jso =ToolsUtil.postHttps(url,paraData.toString(),headers) ;
		if(jso.getInteger("code")!=2000){
			jsoResp.put("respCode", "-1");
			jsoResp.put("respDesc", "jso-openapi/v1-UpdateVideoUploadInfos()封面请求预准备 失败");
			jsoResp.put("respMsg", jso.toString());
			return jsoResp;
		}
		System.out.println("jso-openapi/v1-UpdateVideoUploadInfos()封面请求预准备:"+jso);
	  
		
	   // 获取视频封面	可能会失败，需要反复获取  	    
	    int i=0;
	    String src_uri="";
		while(true)
		{
			   	if(i>videoCoverCount)
		        {
		        	 break;
		        }
			    i++;
			    url = "https://mp.toutiao.com/mp/agw/article_material/video/get_thumb?vid="+vid;						
			    headers = new HashMap<String, String>();		  
				headers.put("Accept", "*/*"); 
				headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
				headers.put("referer", "https://mp.toutiao.com/profile_v3/graphic/publish");   
				headers.put("Host", "mp.toutiao.com");
//				headers.put("Origin", "mp.toutiao.com");
				headers.put("Cookie", cookies);	 			
		        jso =ToolsUtil.getHttps(url,headers) ;				       
		        if("0".equals(jso.getString("code"))&& jso.containsKey("items"))
		        {
		        	    System.out.println("jso-video/get_thumb(获取封面成功)"+i+":"+jso);	
		        	    src_uri=jso.getJSONArray("items").getJSONObject(0).getString("src_uri");
				        System.out.println("src_uri----"+src_uri);
				        paraData = new JSONObject();
				    	paraData.put("sp", "toutiao");
				    	paraData.put("vid", vid);
				    	paraData.put("vu", vid);
				    	paraData.put("vname", file.getName()); //11.mp4
				    	paraData.put("vposter", "http://p1.pstatp.com/large/"+src_uri);
				    	paraData.put("thumb_url", src_uri);
				        jsoResp.put("PGC_VIDEO", "{!-- PGC_VIDEO:"+paraData.toString()+" --}");
				        jsoResp.put("respCode", "0");
				        jsoResp.put("respDesc", "获取封面视频成功");
				        jsoResp.put("respMsg", jso);
				       
				        break;
		        }
		        else
		        {
		        	 System.out.println("jso-video/get_thumb(获取封面失败)"+i+"睡眠5秒钟:"+jso);	
		        	  try {
		  				Thread.sleep(5000);
		  			} catch (InterruptedException e) {
		  				// TODO Auto-generated catch block
		  				e.printStackTrace();
		  			}
		        
				    jsoResp.put("respCode", "-1");
				    jsoResp.put("respDesc", "获取封面视频失败");
				    jsoResp.put("respMsg", jso);
		        	continue;
		        }
		        
		       
		        
		}
		 
		
		return jsoResp;	
       
	}
	
	
	
	
	
	
	
	
	/**	
	 *美篇图片上传
	 */	
	public  static JSONObject  uploadImageFileForMp(String authorToken,JSONObject reqData)			
	{	
	 	String filePath=!reqData.containsKey("filePath") ? "" : reqData.getString("filePath");	 	
        JSONObject jsoResp= new JSONObject();
        JSONObject jsoReq= new JSONObject();
		jsoResp.put("respCode", "-1");
		jsoResp.put("respDesc", "转换异常");	
		File file = new File(filePath);
	    if (!file.exists()) {	     
	        jsoResp.put("respCode", "-1");
			jsoResp.put("respDesc", "路径[" + filePath + "]对应的图片文件不存在!");
	        return jsoResp;
	    }
		
        JSONObject paraData = new JSONObject();		   
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();		  	   
	    file = new File(filePath);
	    String fileName = file.getName();
	    String key="";
	    String token="";
	    String url = "https://www.meipian.cn/api/upload/token";				   
	    fileName=ToolsUtil.get32bituuid();
	    jsoReq.put("file", fileName);
	    jsoReq.put("size", file.length());			  
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/json;charset=UTF-8");
		headers.put("Host", "w.meipian.cn");		
		headers.put("token", authorToken);					
		paraData.put("url", url);
    	paraData.put("method", "POST");
    	paraData.put("stream", jsoReq.toString().getBytes());		        
		jsoResp = ToolsUtil.uploadStream(paraData,headers);			
	    if(0==jsoResp.getJSONObject("result").getInteger("code"))
	    {
	    	System.out.println("jso-upload_token(获取上传空间服务器密钥)--成功:"+jsoResp);	  
	    	key=jsoResp.getJSONObject("result").getJSONObject("data").getString("key");
	    	token=jsoResp.getJSONObject("result").getJSONObject("data").getString("token");
	    	System.out.println("key----"+key+";token----"+token);
	    }    
	    else
	    {
	    	System.out.println("jso-upload_token(获取上传空间服务器密钥)-失败:"+jsoResp);	  
	    	jsoResp.put("respMsg", jsoResp);	    	
			return jsoResp;
	    }
	   
	    url = "https://upload.qiniup.com/";	
	    Map<String, String> textMap = new HashMap<String, String>();
	    //可以设置多个input的name，value
	    textMap.put("token", token);
        textMap.put("key", key);
        textMap.put("fname", fileName);
        //设置file的name，路径
        Map<String, String> fileMap = new HashMap<String, String>();
        fileMap.put("file", filePath);
        String fileType = "";//image/png
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 			
		paraData.put("url", url);
    	paraData.put("method", "POST");		    	 
		jsoResp = ToolsUtil.uploadStream(paraData,headers,textMap,fileMap,fileType);		    
		System.out.println("jso-upload(服务器上传):"+jsoResp);	  
			    
		return jsoResp;
	}
	/**	
	 *美篇视频上传
	 */
	public  static JSONObject  uploadVideoFileForMp(String authorToken,JSONObject reqData)
	{
		String filePath=!reqData.containsKey("filePath") ? "" : reqData.getString("filePath");	 	
        JSONObject jsoResp= new JSONObject();
        JSONObject jsoReq= new JSONObject();
		jsoResp.put("respCode", "-1");
		jsoResp.put("respDesc", "转换异常");	
		File file = new File(filePath);
	    if (!file.exists()) {	     
	        jsoResp.put("respCode", "-1");
			jsoResp.put("respDesc", "路径[" + filePath + "]对应的视频文件不存在!");
	        return jsoResp;
	    }
		
		
	    JSONObject para = new JSONObject();	
	    JSONObject resp = new JSONObject();	
	    para.put("filePath", reqData.getString("filePath"));
	    para.put("ffmpegPath", reqData.getString("ffmpegPath"));
	    para.put("imgPath",  reqData.getString("imgPath"));
	    resp=ToolsUtil.processImg(para);
		if(!"0".equals(resp.getString("respCode")))
		{
			return resp;
		}
		else
		{
			System.out.println("封面图片路径----"+resp.getString("imgPath"));
		}
		
		
		para = new JSONObject();	  
	    para.put("filePath", resp.getString("imgPath"));
	    resp=uploadImageFileForMp(authorToken, para);	   
		if(!"0".equals(resp.getString("respCode")))
		{
			return resp;
		}
		else
		{
			System.out.println("封面上----传成功"+resp);
		}
		
		
        JSONObject paraData = new JSONObject();		   
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();		  	   
	    file = new File(filePath);
	    String fileName = file.getName();
	    String key="";
	    String token="";
	    String  url = "https://www.meipian.cn/api/upload/videoToken";				   
	    fileName=ToolsUtil.get32bituuid();
	    jsoReq.put("file", fileName);
	    jsoReq.put("size", file.length());			  
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/json;charset=UTF-8");
		headers.put("Host", "w.meipian.cn");		
		headers.put("token", authorToken);					
		paraData.put("url", url);
    	paraData.put("method", "POST");
    	paraData.put("stream", jsoReq.toString().getBytes());		        
		jsoResp = ToolsUtil.uploadStream(paraData,headers);			
	    if(0==jsoResp.getJSONObject("result").getInteger("code"))
	    {
	    	System.out.println("jso-upload_token(获取上传空间服务器密钥)--成功:"+jsoResp);	  
	    	key=jsoResp.getJSONObject("result").getJSONObject("data").getString("key");
	    	token=jsoResp.getJSONObject("result").getJSONObject("data").getString("token");
	    	System.out.println("key----"+key+";token----"+token);
	    }    
	    else
	    {
	    	System.out.println("jso-upload_token(获取上传空间服务器密钥)-失败:"+jsoResp);	  
	    	jsoResp.put("respMsg", jsoResp);	    	
			return jsoResp;
	    }
	   
	    url = "https://upload.qiniup.com/";	
	    Map<String, String> textMap = new HashMap<String, String>();
	    //可以设置多个input的name，value
	    textMap.put("token", token);
        textMap.put("key", key);
        textMap.put("fname", fileName);
        //设置file的name，路径
        Map<String, String> fileMap = new HashMap<String, String>();
        fileMap.put("file", filePath);
        String fileType = "";//image/png
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 			
		paraData.put("url", url);
    	paraData.put("method", "POST");		    	 
		jsoResp = ToolsUtil.uploadStream(paraData,headers,textMap,fileMap,fileType);		    
		System.out.println("jso-upload(服务器上传):"+jsoResp);	 
		if(!"0".equals(resp.getString("respCode")))
		{
			return jsoResp;
		}
		else
		{
			jsoResp.put("imgCorver", resp);
		}
			    
		return jsoResp;
		
	}
	

	
	/**
	 * 两部路图片，视频上传
	 */
	public  static JSONObject  uploadFileFor2bulu(String authorToken,JSONObject reqData)			
	{	
	 	String filePath=!reqData.containsKey("filePath") ? "" : reqData.getString("filePath");		
	 	boolean isConvert=!reqData.containsKey("isConvert") ? false : reqData.getBoolean("isConvert");	
        JSONObject jsoResp= new JSONObject();
        JSONObject jsoReq= new JSONObject();
		jsoResp.put("respCode", "-1");
		jsoResp.put("respDesc", "转换异常");	
		File file = new File(filePath);
	    if (!file.exists()) {	     
	        jsoResp.put("respCode", "-1");
			jsoResp.put("respDesc", "路径[" + filePath + "]对应的图片文件不存在!");
	        return jsoResp;
	    }
		
        JSONObject paraData = new JSONObject();		   
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();		  	   
	    file = new File(filePath);
	    String fileName = file.getName();
	    String fileType = "";//image/png
	    String url = "";				   
	    fileName=ToolsUtil.get32bituuid();	  
	    Map<String, String> textMap = new HashMap<String, String>();
	    Map<String, String> fileMap = new HashMap<String, String>();
	    if(isConvert)
	    {
	    	
	    	url = "http://www.2bulu.com/file/upload.htm";
	    	textMap.put("id", ToolsUtil.get32bituuid());	      
	        //设置file的name，路径	    
	        fileMap.put("file", filePath);  
	    }
	    else
	    {
	    	url = "http://www.2bulu.com/file/upload_ueditor_art.htm?encode=utf-8";	
	    	//可以设置多个input的name，value
		    textMap.put("id", ToolsUtil.get32bituuid());
	        textMap.put("name", fileName);
	        textMap.put("type", fileType);
	        textMap.put("lastModifiedDate", "");
	        textMap.put("size", String .valueOf(file.length()));
	        //设置file的name，路径	    
	        fileMap.put("upfile", filePath);  
	    }	    
	    
	  
	       
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 	
		headers.put("Host", "www.2bulu.com");		
		headers.put("Cookie", authorToken);					
		paraData.put("url", url);
    	paraData.put("method", "POST");
    	paraData.put("stream", jsoReq.toString().getBytes());	    	 
		jsoResp = ToolsUtil.uploadStream(paraData,headers,textMap,fileMap,fileType);		    
		System.out.println("jso-upload2buluFile(服务器上传):"+jsoResp);	  
			    
		return jsoResp;
	}
	
	/**	
	 *新浪图片上传
	 */	
	public  static JSONObject  uploadImageFileForSina(String cookies,JSONObject reqData)			
	{	
	 	String filePath=!reqData.containsKey("filePath") ? "" : reqData.getString("filePath");	 	
	 	String type=!reqData.containsKey("type") ? "local" : reqData.getString("type");	 		 	
	 	JSONObject jsoResp= new JSONObject();    
		jsoResp.put("respCode", "-1");
		jsoResp.put("respDesc", "转换异常");	
		byte[] stream = new byte[1024];
		Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();	
		if("local".equals(type)){
			File file = new File(filePath);
		    if (!file.exists()) {	     
		        jsoResp.put("respCode", "-1");
				jsoResp.put("respDesc", "路径[" + filePath + "]对应的图片文件不存在!");
		        return jsoResp;
		    }
			file = new File(filePath);
		    stream=ToolsUtil.fileConvertToByteArray(new File(filePath));
		}else{
			try{
				URI uri = new URI(filePath);
				CloseableHttpClient httpClient = HttpClients.custom().build();
				HttpEntity entity = httpClient.execute(RequestBuilder.get().setUri(uri).build()).getEntity();
				stream=EntityUtils.toByteArray(entity);	
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		//	    String url = "https://picupload.weibo.com/interface/pic_upload.php?app=miniblog&s=json&p=1&data=1&url=weibo.com%2Fu%2F5525302413&markpos=1&logo=1&nick=%E5%8D%97%E7%93%9C%E6%95%99%E4%B8%BB007&file_source=4";
		String url = "https://picupload.weibo.com/interface/pic_upload.php?app=miniblog&s=json&p=1&data=1&url=&markpos=1&logo=1&nick=&file_source=4";			
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/x-www-form-urlencoded");
		headers.put("Host", "picupload.weibo.com");		
		headers.put("Cookie", cookies);		
		JSONObject paraData = new JSONObject();	
		paraData.put("url", url);
    	paraData.put("method", "POST");
    	paraData.put("stream",stream);    	
    	headers.put("Content-Length", stream.length+"");      	
		jsoResp = ToolsUtil.uploadStream(paraData,headers);		 			    
		return jsoResp;
	}	
	
	
	/**
	 * 新浪图片上传
	 * @param imgList
	 * @param cookies
	 * @return
	 */
	public static JSONObject getNewHtmlImgUrlForSina(List<String> imgList,String cookies)
	{
		JSONObject imgNewArray = new JSONObject();
		
		for(String img:imgList){	
			URI uri;
			try {
				uri = new URI(img);
				CloseableHttpClient httpClient = HttpClients.custom().build();

				HttpEntity entity = httpClient.execute(RequestBuilder.get().setUri(uri).build()).getEntity();
			
				JSONObject paraData = new JSONObject();		   
			    Map<String, String> headers = new HashMap<String, String>();
			    headers = new HashMap<String, String>();		   
			    String url = "https://picupload.weibo.com/interface/pic_upload.php?app=miniblog&s=json&p=1&data=1&url=weibo.com%2Fu%2F5525302413&markpos=1&logo=1&nick=%E5%8D%97%E7%93%9C%E6%95%99%E4%B8%BB007&file_source=4";			
			    byte[] stream=EntityUtils.toByteArray(entity);		   		   
			    headers = new HashMap<String, String>();				
				headers.put("Accept", "*/*"); 
				headers.put("Content-Type", "application/x-www-form-urlencoded");
				headers.put("Host", "picupload.weibo.com");		
				headers.put("Cookie", cookies);					
				paraData.put("url", url);
		    	paraData.put("method", "POST");
		    	paraData.put("stream",stream );    	
		    	headers.put("Content-Length", stream.length+""); 	    	
		    	JSONObject ret = ToolsUtil.uploadStream(paraData,headers);	
		    	JSONObject jsoResp=ret.getJSONObject("result");
		    	String domain="https://wx2.sinaimg.cn/large/";
		    	if(jsoResp!=null&&"A00006".equals(jsoResp.getString("code"))){
					imgNewArray.put(img, domain+jsoResp.getJSONObject("data").getJSONObject("pics").getJSONObject("pic_1").getString("pid"));
				}else{
					imgNewArray.put(img, "");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				 	
		}
		
		return 	imgNewArray;
	}
	
	
	/**
	 * 创建，生成文章id
	 * 保存文章
	 * 发布文章
	 * */
	public  static JSONObject sendArticleForSina(JSONObject reqData)			
	{	
	 	String uid=!reqData.containsKey("uid") ? "" : reqData.getString("uid");		
	 	String  cookies=!reqData.containsKey("cookies") ? "" : reqData.getString("cookies");	
		String  htmlCont=!reqData.containsKey("htmlCont") ? "" : reqData.getString("htmlCont");	
		String  title=!reqData.containsKey("title") ? "title" : reqData.getString("title");	
		String  summary=(!reqData.containsKey("summary")||reqData.getString("summary")==null)? "summary" : reqData.getString("summary");	
		String  conver=!reqData.containsKey("conver") ? "" : reqData.getString("conver");			
		//htmlCont="%3Cp%20align%3D%22justify%22%3Eb%3C%2Fp%3E%0A%20%20%20%20%20%20%20%20%20%20%3Cimg%20src%3D%22https%3A%2F%2Fwx2.sinaimg.cn%2Flarge%2F007oE9GAly1gerrsy3ya9j30cq0673yf.jpg%22%20%2F%3E";
		JSONObject paraData = new JSONObject();		   
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();	  	   
	    String id="";
	    String url = "https://card.weibo.com/article/v3/aj/editor/draft/create";	     
	    String reqCont="uid="+uid;
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "application/json, text/plain, */*"); 
		headers.put("Content-Type", "application/x-www-form-urlencoded");
		headers.put("Host", "card.weibo.com");		
		headers.put("Referer", "https://card.weibo.com/article/v3/editor");	
		headers.put("Cookie", cookies);	
		paraData.put("url", url);
    	paraData.put("method", "POST");
        paraData.put("content", reqCont);      
        JSONObject jsoResp = ToolsUtil.uploadStream(paraData,headers);	   
	    if(100000==jsoResp.getJSONObject("result").getInteger("code"))	    {
	    	System.out.println("sendArticleIdForSina(创建文章id)--成功:"+jsoResp);	  
	    	
	    	 id=jsoResp.getJSONObject("result").getJSONObject("data").getString("id");	    	
	    	 jsoResp.put("id", id);	    	
	    }    
	    else
	    {
	    	System.out.println("createArticleIdForSina(创建新浪文章id)--失败:"+jsoResp);	  
	    	jsoResp.put("respMsg", jsoResp);	    	
			return jsoResp;
	    }
	    
	    url = "https://card.weibo.com/article/v3/aj/editor/draft/save?uid="+uid+"&id="+id; //	6777132244					
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("Host", "card.weibo.com");		
		headers.put("Cookie", cookies);		
		headers.put("Referer", "https://card.weibo.com/article/v3/editor");	
		htmlCont = StringEscapeUtils.unescapeHtml(htmlCont);
		
		String content="title="+title+"&type=&summary="+summary+"&writer=&cover="+conver+"&content="+htmlCont+"&collection=%5B%5D&updated=&id="+id+"&subtitle=&extra=null&status=0&publish_at=&error_msg=&error_code=0&free_content=&is_word=0&article_recommend=%5B%5D&follow_to_read=1&isreward=1&pay_setting=%7B%22ispay%22%3A0%2C%22isvclub%22%3A0%7D&source=0&action=1&content_type=0&save=1"; 
        jsoResp =ToolsUtil.postHttps(url, content, headers) ;	
        if(100000==jsoResp.getInteger("code"))	    {
	    	System.out.println("sendArticleIdForSina(保存新浪文章)--成功:"+jsoResp);	  
	    	
	    	
	    }    
	    else
	    {
	    	System.out.println("createArticleIdForSina(保存新浪文章)--失败:"+jsoResp);	  
	    	jsoResp.put("respMsg", jsoResp);	    	
			return jsoResp;
	    }
	
        
	    url = "https://card.weibo.com/article/v3/aj/editor/draft/publish?uid="+uid+"&id="+id;	//	6777132244				
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("Host", "card.weibo.com");		
		headers.put("Cookie", cookies);		
		headers.put("Referer", "https://card.weibo.com/article/v3/editor");	
		String text="发布了头条文章：《"+title+"》 ";
		content="id="+id+"&rank=0&text="+text+"&sync_wb=0&is_original=0&time="; 
        jsoResp =ToolsUtil.postHttps(url, content, headers) ;	
        if(100000==jsoResp.getInteger("code"))	    {
	    	System.out.println("sendArticleIdForSina(发送新浪文章)--成功:"+jsoResp);	  
	    	
	    	
	    }    
	    else
	    {
	    	System.out.println("sendArticleIdForSina(发送新浪文章)--失败:"+jsoResp);	  
	    	jsoResp.put("respMsg", jsoResp);	    	
			return jsoResp;
	    }
	
			    
	    return jsoResp;
	}
	
	/**
	 * https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=1589763438302
	 * */	
	public  static JSONObject sendBlogForSina(JSONObject reqData)			
	{	
	 	String uid=!reqData.containsKey("uid") ? "" : reqData.getString("uid");		
	 	String  cookies=!reqData.containsKey("cookies") ? "" : reqData.getString("cookies");	
		String  htmlCont=!reqData.containsKey("htmlCont") ? "" : reqData.getString("htmlCont");	
		String  title=!reqData.containsKey("title") ? "title" : reqData.getString("title");	
		String  summary=(!reqData.containsKey("summary")||reqData.getString("summary")==null)? "summary" : reqData.getString("summary");	
		String  pic_id=!reqData.containsKey("pic_id") ? "" : reqData.getString("pic_id");	   
		//007oE9GAly1gewcar56nyj308m046mwx|007oE9GAly1gewcaybn5qj308m046mwx

		Map<String, String> headers = new HashMap<String, String>();	    
	    String  url = "https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=1589763438302";					
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("Host", "weibo.com");		
		headers.put("Cookie", cookies);		
		headers.put("Referer", "https://weibo.com/u/"+uid+"/home?is_pic=1");	
		htmlCont = StringEscapeUtils.unescapeHtml(htmlCont);
		
		String content="location=v6_content_home&text="+htmlCont+"&appkey=&style_type=1&pic_id="+pic_id+"&tid=&pdetail=&mid=&isReEdit=false&gif_ids=&rank=0&rankid=&module=stissue&pub_source=main_&updata_img_num=&pub_type=dialog&isPri=null&_t=0"; 
		JSONObject  jsoResp =ToolsUtil.postHttps(url, content, headers) ;	
        if(100000==jsoResp.getInteger("code"))	    {
        	System.out.println("sendArticleIdForSina(发送新浪文章)--成功:"+jsoResp);
	    	
	    	
	    }    
	    else
	    {
	    	System.out.println("sendArticleIdForSina(发送新浪文章)--失败:"+jsoResp);	    
	    	jsoResp.put("respMsg", jsoResp);	    	
			return jsoResp;
	    }
	
        
	  
			    
	    return jsoResp;
	}
	
	
	public static void main(String[] args)
	{
		
		
			 String cookies="SINAGLOBAL=5237768945223.389.1571881883203; un=15522361396; wvr=6; wb_timefeed_6777132244=1; UOR=www.baidu.com,weibo.com,login.sina.com.cn; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9Whe57DLMgVVCsK6Ic1Ipdyo5JpX5KMhUgL.FoqNS0Mpe0zEShB2dJLoIEBLxKBLB.zL122LxKqL1K.L1-2LxKqL1h.L1KnLxKMLB--LBK2t; Ugrow-G0=6fd5dedc9d0f894fec342d051b79679e; ALF=1621299094; SSOLoginState=1589763095; SCF=ArSy3JLAY1vvTl0oG0MycRCeeu1ChdORRf6j36WQH2eOdb7dy0mQIjHeSKLs1Zi5k4iAkxvpmdovzrQ_UQem4cE.; SUB=_2A25zxaxHDeRhGeBJ7FUQ8yzOzziIHXVQspqPrDV8PUNbmtCOLXfWkW9NRiNDiRqIAJ49O0_MV-CNCtW3sSsaEXq1; SUHB=0YhhOIFbopbr7c; TC-V5-G0=4de7df00d4dc12eb0897c97413797808; wb_view_log_6777132244=1360*7681; _s_tentry=login.sina.com.cn; Apache=3710317254662.4517.1589763132469; ULV=1589763132579:8:7:1:3710317254662.4517.1589763132469:1589524211086; TC-Page-G0=8afba920d7357d92ddd447eac7e1ec5c|1589767759|1589767759; webim_unReadCount=%7B%22time%22%3A1589767852425%2C%22dm_pub_total%22%3A0%2C%22chat_group_client%22%3A0%2C%22chat_group_notice%22%3A0%2C%22allcountNum%22%3A0%2C%22msgbox%22%3A0%7D";
			 String url="https://weibo.com/aj/onoff/getstatus?ajwvr=6&sid=0&__rnd=1589781179421";
			 Map<String, String> headers = new HashMap<String, String>();	   
			 headers.put("Accept", "*/*"); //*/*
			 headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
			 headers.put("Host", "weibo.com");		
			 headers.put("Cookie", cookies);	
			 JSONObject  jsoResp=ToolsUtil.getHttps(url, headers);
			 System.out.println(jsoResp);
			 
//			 String uid="6777132244";
//			 JSONObject reqData= new JSONObject();
//			 reqData.put("uid", uid);
//			 reqData.put("tilte", "文章标题");
//			 reqData.put("summary", "文章摘要");
//			 reqData.put("pic_id", "007oE9GAly1gewcar56nyj308m046mwx|007oE9GAly1gewcaybn5qj308m046mwx");
//			 reqData.put("htmlCont", "aaa文章内容111111");
//			 reqData.put("cookies", cookies);
//			 JSONObject jsoResp = new JSONObject();	
//			 jsoResp=sendBlogForSina(reqData);
//			 System.out.println(jsoResp);
			
		  
//           List<String> imgList = new ArrayList<String>();
//		     imgList.add("https://img.t.sinajs.cn/t5/skin/public/profile_cover/001_s.jpg");
//		     imgList.add("https://mu1.sinaimg.cn/frame.180x240/weiyinyue.music.sina.com.cn/movie_poster/186939_vertical.jpg");
//		     JSONObject ret=getNewHtmlImgUrlForSina(imgList,authorToken);
//		     System.out.println(ret.toJSONString());
		 
		 
//         https://wx2.sinaimg.cn/large/007oE9GAly1gert2mnyabj307602674c
//         https://wx2.sinaimg.cn/large/007oE9GAly1gert2mv060j304i06omx5
//		 String authorToken="SINAGLOBAL=5237768945223.389.1571881883203; un=15522361396; wvr=6; UOR=www.baidu.com,weibo.com,login.sina.com.cn; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9Whe57DLMgVVCsK6Ic1Ipdyo5JpX5KMhUgL.FoqNS0Mpe0zEShB2dJLoIEBLxKBLB.zL122LxKqL1K.L1-2LxKqL1h.L1KnLxKMLB--LBK2t; ALF=1620953086; SSOLoginState=1589417086; SCF=ArSy3JLAY1vvTl0oG0MycRCeeu1ChdORRf6j36WQH2eOp1Wt61KV_pnbTI_FpmSDP3B4QtA7SjTqOKLFxb6NfzE.; SUB=_2A25zuOQvDeRhGeBJ7FUQ8yzOzziIHXVQzFLnrDV8PUNbmtAKLWr3kW9NRiNDiQyPA0IUJx-h7bU5lyjVC5QEmCri; SUHB=0JvQF1ZV4ZOyN3; _s_tentry=login.sina.com.cn; Apache=941456837887.7175.1589417097265; ULV=1589417097360:5:4:4:941456837887.7175.1589417097265:1589361181179; CARD-MAIN=e2379342ceb6c9c8726a496a5565689e; webim_unReadCount=%7B%22time%22%3A1589417306357%2C%22dm_pub_total%22%3A0%2C%22chat_group_client%22%3A0%2C%22chat_group_notice%22%3A0%2C%22allcountNum%22%3A0%2C%22msgbox%22%3A0%7D";
//		 String filePath="D:\\pic.png";
//		 JSONObject reqData = new JSONObject();	
//		 JSONObject jsoResp = new JSONObject();	
//		 reqData.put("filePath", filePath);	
//		 jsoResp=ProtocolUtil.uploadImageFileForSina(authorToken, reqData);
//		 System.out.println(jsoResp);
//		
//		 String authorToken="testkey=--null--; sysop_privilege_key=ac724af93c579af00000b5b945f7bffe; sysop_privilege_user_name=%E5%88%98%E4%BA%9A%E6%A5%A0; sysop_privilege_nick_name=http%3A%2F%2Fthirdwx.qlogo.cn%2Fmmopen%2Fvi_32%2FQ0j4TwGTfTJIocn8OMjfSLlZprnFJ4nFpDtDb04qF6FJQCssLHpJqJr6yGNqqINYTUOXIzqwxSmIqZQQD0WibNQ%2F132; Hm_lvt_bf0fe262e26a6d19efeec76b5edd8d9e=1587868380,1587968411; JSESSIONID=AD2B084A1F4D34063C5E392DC73F6A81; Hm_lpvt_bf0fe262e26a6d19efeec76b5edd8d9e=1587968429";
//		 String filePath="D:\\1.jpg";
//		 JSONObject reqData = new JSONObject();	
//		 JSONObject jsoResp = new JSONObject();	
//		 reqData.put("filePath", filePath);
//		 reqData.put("isConvert", false);
//		 jsoResp=ProtocolUtil.upload2buluFile(authorToken, reqData);
//		 System.out.println(jsoResp);

	      

	}
	
	public static void main3(String[] args)
	{

	     
		 String authorToken="R6sBPUIbGcqIK4hkMuT8WRLVnbuYOpFW";
		 String filePath="D:\\1.mp4";
		 JSONObject reqData = new JSONObject();	
		 JSONObject jsoResp = new JSONObject();	
		 reqData.put("filePath", filePath);
		 reqData.put("ffmpegPath", "D:\\workplace\\.metadata\\.me_tcat7\\webapps\\weimonitor\\view\\file\\ffmpeg\\ffmpeg.exe");
		 reqData.put("imgPath", "D:\\workplace\\.metadata\\.me_tcat7\\webapps\\weimonitor\\view\\file\\image1");		 
		 jsoResp=ProtocolUtil.uploadVideoFileForMp(authorToken, reqData);
		 System.out.println(jsoResp);
	}
	
		

	
	public static void main2(String[] args)
	{
		  
		    
		    String content="";
		    String cookies="passport_csrf_token=6921d8ce370295d4f9d68bfff840e202;s_v_web_id=verify_k8qoaovq_f164B7VF_YiqQ_4WIN_BH8Z_07Dsfga5wKNL;sso_auth_status=c73d552366efd68ef87dfcfb2b7d51e9;sso_uid_tt=998a1307e1ec8ca7329b0e53f8ef3180;sso_uid_tt_ss=998a1307e1ec8ca7329b0e53f8ef3180;toutiao_sso_user=6aba4914ccd86eb6595ff0550de8a8f2;toutiao_sso_user_ss=6aba4914ccd86eb6595ff0550de8a8f2;passport_auth_status=74cd2aa025ce22a57f0e35605f4958d8%2C219ae1dfd2ee07ed455cd0752c9616a4;sid_guard=9bef42d870d744aa76169b5dedeb45ed%7C1586310501%7C5184000%7CSun%2C+07-Jun-2020+01%3A48%3A21+GMT;uid_tt=8f71874e3f3ca9a188fc86537f89b8c9;uid_tt_ss=8f71874e3f3ca9a188fc86537f89b8c9;sid_tt=9bef42d870d744aa76169b5dedeb45ed;sessionid=9bef42d870d744aa76169b5dedeb45ed;sessionid_ss=9bef42d870d744aa76169b5dedeb45ed;gftoken=OWJlZjQyZDg3MHwxNTg2MzEwNTAxNjh8fDAGBgYGBgY;SLARDAR_WEB_ID=9bd57122-94ce-4497-9675-52f309eef873;tt_webid=6813151764553516552";	
			JSONObject jsoReq=new JSONObject();
			JSONObject jsoResp=new JSONObject();
			String filePath="D:\\video\\test2.mp4";
			int size=400000;
			jsoReq.put("videoCoverCount", 5);
			jsoReq.put("filePath", filePath);	
			jsoReq.put("size", size);
			
			jsoResp=uploadVideoFileForTT(cookies,jsoReq);
			
			HashMap<String, String> headers = new HashMap<String, String>();
		    String  url = "http://mp.toutiao.com/mp/agw/article/publish?source=mp&type=article";		
	        headers = new HashMap<String, String>();		  
			headers.put("Accept", "*/*"); //*/*
			headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
			headers.put("referer", "https://mp.toutiao.com/profile_v3/graphic/publish");   
			headers.put("Host", "mp.toutiao.com");
//			headers.put("Origin", "mp.toutiao.com");
			headers.put("Cookie", cookies);	 	
			
			
			
			String htmlCont="<p>头条视频测试</p><p><br></p><p>";		        
			
				
		    String title="头条视频测试2";
		    String pgc_feed_covers="";
		    htmlCont+=jsoResp.getString("PGC_VIDEO");
		    htmlCont+="</p>";

		    content="article_type=0&source=0&title="+title+"&content="+htmlCont+"&title_id=1585641653970_1662117622830093&extra=&educluecard=&pay_question_card=";
		    content+="&pgc_feed_covers="+pgc_feed_covers+"&claim_origin=0&origin_debut_check_pgc_normal=0&qy_self_recommendation=0&is_fans_article=0";
		    content+="&govern_forward=0&praise=0&disable_praise=0&article_ad_type=2&tree_plan_article=0&activity_tag=0";
		    content+="&community_sync=0&save=1&timer_status=0&timer_time=";
	      
//		    JSONObject jso =ToolsUtil.postHttps(url, content, headers) ;
//	        System.out.println("jso-article/publish(上传):"+jso);	
			
			
		
	}
	
	public static void main1(String[] args)
	{
		JSONObject jsobject = new JSONObject();
//		 HttpUtils.getHttpsHtml(url, null)
		String cookies="s_v_web_id=fd7f003a2268bb0632f5be31b7122263; sso_auth_status=ab98c54b624af27362330180c74b5e10; sso_uid_tt=039dc059ac5ff19669627549cb2dcf18; sso_uid_tt_ss=039dc059ac5ff19669627549cb2dcf18; toutiao_sso_user=1a2bfee37ae434de9ce5ee8d4a171080; toutiao_sso_user_ss=1a2bfee37ae434de9ce5ee8d4a171080; passport_auth_status=8c914a1cbabb7ec59d33384011b539bf%2Cb17855cc278a7887e0c99dc1dfa89690; sid_guard=1b8b1a044c3cf8648eef036d231147f7%7C1585634459%7C5184000%7CSat%2C+30-May-2020+06%3A00%3A59+GMT; uid_tt=1428fda05b092f1d5f227fb227abb083; uid_tt_ss=1428fda05b092f1d5f227fb227abb083; sid_tt=1b8b1a044c3cf8648eef036d231147f7; sessionid=1b8b1a044c3cf8648eef036d231147f7; sessionid_ss=1b8b1a044c3cf8648eef036d231147f7; gftoken=MWI4YjFhMDQ0Y3wxNTg1NjM5NTM1MjB8fDAGBgYGBgY; SLARDAR_WEB_ID=520fffad-7a19-4190-8af7-7e4107729303";	
		String htmlCont="";
		htmlCont="<body><p>testtest</p>"+
		"<div class=\"pgc-img\">"+
		"<img class=\"\" src=\"https://p6-tt.byteimg.com/large/pgc-image/d24f992044fe4a059d6a549511e9a6d6\" >a"+		
		"<img class=\"\" src=\"http://p1.pstatp.com/origin/pgc-image/bce07aad8bb34fa59815b4ffd0e95420\" >b"+
		"<p class=\"pgc-img-caption\"></p>"+
		"</div></body>";
		htmlCont=ToolsUtil.HtmlLableClear(htmlCont);	
		List<String> imgList = new ArrayList<>();	
		imgList=ToolsUtil.getHtmlImgUrl(htmlCont);
		String coverImg="default.jpg";
		if(imgList.size()>0){
			JSONObject imgArray = new JSONObject();			
			imgArray=getNewHtmlImgUrlForTT(imgList,cookies);	
			coverImg = (String)imgArray.keySet().toArray()[0];
			htmlCont=ToolsUtil.replaceHtmlImgUrl(htmlCont,imgArray);
		}	
		
		try
		{		    
			   
			    String imgUrl="https://img-blog.csdn.net/20180628200143779?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zOTcyMzM1Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70";
		        JSONArray jarray= getConverImgUrlForTT(imgUrl,cookies);
		        log.info("getConverImgUrl: "+jarray);
		        String pgc_feed_covers="";
		        if(jarray!=null)
		        	pgc_feed_covers=jarray.toString();
			
			    String url = "http://mp.toutiao.com/mp/agw/article/publish?source=mp&type=article";						
			    Map<String, String> headers = new HashMap<String, String>();
			    headers = new HashMap<String, String>();
				headers.put("Accept", "*/*"); //*/*
				headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
				headers.put("referer", "https://mp.toutiao.com/profile_v3/graphic/publish");   
				headers.put("Host", "mp.toutiao.com");
//				headers.put("Origin", "mp.toutiao.com");
				headers.put("Cookie", cookies);					
				
		        String content="";		        
		        String title="测试中国abc";
//		        content="article_type=0&source=0&title=测试122323&content=333中国23434&title_id=1585641653970_1662117622830093&extra=&educluecard=&pay_question_card=";
//			    content+="&pgc_feed_covers=&claim_origin=0&origin_debut_check_pgc_normal=0&qy_self_recommendation=0&is_fans_article=0";
//			    content+="&govern_forward=0&praise=0&disable_praise=0&article_ad_type=2&tree_plan_article=0&activity_tag=0";
//			    content+="&community_sync=0&save=1&timer_status=0&timer_time=";
			    
			   
//		        [{"id":"","url":"http://p1.pstatp.com/list/pgc-image/3c967326d8344a3895264df566fce2c1","uri":"pgc-image/3c967326d8344a3895264df566fce2c1","ic_uri":"","thumb_width":481,"thumb_height":518}]
			    //时间戳_appid
		        content="article_type=0&source=0&title="+title+"&content="+htmlCont+"&title_id=1585641653970_1662117622830093&extra=&educluecard=&pay_question_card=";
			    content+="&pgc_feed_covers="+pgc_feed_covers+"&claim_origin=0&origin_debut_check_pgc_normal=0&qy_self_recommendation=0&is_fans_article=0";
			    content+="&govern_forward=0&praise=0&disable_praise=0&article_ad_type=2&tree_plan_article=0&activity_tag=0";
			    content+="&community_sync=0&save=1&timer_status=0&timer_time=";
		      
		        JSONObject entityjson =ToolsUtil.postHttps(url, content, headers) ;
	
			    System.out.println(entityjson.toString());
			
		}
		catch(Exception e)
     	{
 		
     	}
			
	}
	
	
	
	
	/**
	 * 头条视频上传
	 * jsoReq: {"filePath":"","videoCoverCount":11,"size":500000}
	 */
	public  static JSONObject  uploadVideoFileForDY(String cookies,JSONObject jsoReq)
	{
		
		JSONObject jsoResp= new JSONObject();
		JSONObject fileInfo = new JSONObject();   // {"name":"","path":"",ext:"","size":"","crc":""}
	    String filePath=jsoReq.getString("filePath");
	    File file = new File(filePath);
		if(file.exists()&&file.isFile()){
			try{
				String name = file.getName();
				fileInfo.put("name", name);
				fileInfo.put("path", filePath);
				fileInfo.put("ext", name.indexOf(".")>-1?name.split("\\.")[1]:"");
				fileInfo.put("size", String.valueOf(file.length()));
				if(jsoReq.getInteger("size")==0){
					fileInfo.put("crc", ToolsUtil.getFileCRCCode(file));
				}
			}catch(Exception e){
				e.printStackTrace();
				jsoResp.put("respCode","-1");
				jsoResp.put("respDesc", "获取"+filePath+" 文件信息异常");
				jsoResp.put("respMsg", "获取"+filePath+" 文件信息异常");
				return jsoResp;
			}
		}else{
			jsoResp.put("respCode","-1");
			jsoResp.put("respDesc", filePath+" 文件不存在");
			jsoResp.put("respMsg", filePath+" 文件不存在");
			return jsoResp;
		}
	    
	  
	    String authorization="";
	    String x_tt_access="";
	    String content="";
	    String fileSize=fileInfo.getString("size"); // "4925317"
	    
	  //  String cookies="passport_csrf_token=6921d8ce370295d4f9d68bfff840e202;s_v_web_id=verify_k8qoaovq_f164B7VF_YiqQ_4WIN_BH8Z_07Dsfga5wKNL;sso_auth_status=c73d552366efd68ef87dfcfb2b7d51e9;sso_uid_tt=998a1307e1ec8ca7329b0e53f8ef3180;sso_uid_tt_ss=998a1307e1ec8ca7329b0e53f8ef3180;toutiao_sso_user=6aba4914ccd86eb6595ff0550de8a8f2;toutiao_sso_user_ss=6aba4914ccd86eb6595ff0550de8a8f2;passport_auth_status=74cd2aa025ce22a57f0e35605f4958d8%2C219ae1dfd2ee07ed455cd0752c9616a4;sid_guard=9bef42d870d744aa76169b5dedeb45ed%7C1586310501%7C5184000%7CSun%2C+07-Jun-2020+01%3A48%3A21+GMT;uid_tt=8f71874e3f3ca9a188fc86537f89b8c9;uid_tt_ss=8f71874e3f3ca9a188fc86537f89b8c9;sid_tt=9bef42d870d744aa76169b5dedeb45ed;sessionid=9bef42d870d744aa76169b5dedeb45ed;sessionid_ss=9bef42d870d744aa76169b5dedeb45ed;gftoken=OWJlZjQyZDg3MHwxNTg2MzEwNTAxNjh8fDAGBgYGBgY;SLARDAR_WEB_ID=9bd57122-94ce-4497-9675-52f309eef873;tt_webid=6813151764553516552";	
	 //	cookies="_ga=GA1.2.24301998.1584663458; install_id=0; sid_tt=d0a9a08842fb7d25b12fbea597240f86; sid_guard=d0a9a08842fb7d25b12fbea597240f86; sessionid=d0a9a08842fb7d25b12fbea597240f86; tt_webid=6810540743864059400; uid_tt=7db44c10f675780eb908c389f7c043e22dd149dff269cd4582a57c9ad6ee0e68; gftoken=ZDBhOWEwODg0MnwxNTg1NzA2NjcyNjN8fDAGBgYGBgY; SLARDAR_WEB_ID=1c7e18ec-ad55-4b57-9803-3e32fc17c539; passport_csrf_token=c4c13338cb91b256b557a5899a636b2e; sso_uid_tt=44388a0206e4f50fcab45d3ed3b0ea75; sso_uid_tt_ss=44388a0206e4f50fcab45d3ed3b0ea75; toutiao_sso_user=c95f7b9c8e7cf2008f7e914a0d3f6756; toutiao_sso_user_ss=c95f7b9c8e7cf2008f7e914a0d3f6756; s_v_web_id=k8r1steo_hNzQboyc_ASbU_4RuG_AGAp_aY9Bsqq6v3Zy";	    
	    String url = "https://media.douyin.com/web/api/media/upload/auth/";						
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("referer", "https://creator.douyin.com/content/");   
		headers.put("Host", "creator.douyin.com");
		headers.put("Origin", "creator.douyin.com");
		headers.put("Cookie", cookies);	 			
        JSONObject jso =ToolsUtil.getHttps(url,headers) ;
        System.out.println("jso-upload_token(获取上传空间服务器密钥):"+jso);	 
        if(jso.getInteger("status_code")!=0){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-upload_token 获取上传空间服务器密钥失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        
        authorization=jso.getString("auth");
        try
        {
        	x_tt_access=authorization.split(":")[3];
        }
        catch(Exception e)
        {
        	x_tt_access="56649dadda5ee92f4a264bb3ed2ea908";
        	e.printStackTrace();
        }
	    
	    
        headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/json");
		headers.put("authorization", authorization); 	
		headers.put("x-tt-access", x_tt_access); 			
        url = "https://vas-lf-x.snssdk.com/video/openapi/v1/?action=GetVideoUploadParams&s=6m6u6iw2r04&use_edge_node=1&file_size="+fileSize;						
        jso =ToolsUtil.getHttps(url,headers) ;
        System.out.println("jso-openapi/v1(获取上传空间服务器许可证):"+jso);
        if(jso.getInteger("code")!=2000){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-openapi/v1 获取上传空间服务器许可证失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        String oid=jso.getJSONObject("data").getString("oid");
        String tos_sign=jso.getJSONObject("data").getString("tos_sign");
        String tos_host=jso.getJSONObject("data").getString("tos_host");
        String vid=jso.getJSONObject("data").getString("vid");
        String token=jso.getJSONObject("data").getString("token");
        System.out.println("vid----"+vid);
        System.out.println("token----"+token);


        headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary8wFdKGjSgjw3wIfx");
		headers.put("authorization", tos_sign); 			
		headers.put("Host", tos_host);		 
	    url = "https://"+tos_host+"/"+oid+"?uploads";
	    content="------WebKitFormBoundary8wFdKGjSgjw3wIfx--";
	    jso =ToolsUtil.postHttps(url,content,headers) ;	  
	    if(jso.getInteger("success")!=0){
	    	jsoResp.put("respCode", "-1");
	    	jsoResp.put("respDesc", "jso-uploads 文件生成上传id失败");
	    	jsoResp.put("respMsg", jso.toString());
	    	return jsoResp;
	    }
	    System.out.println("jso-uploads(文件服务器生成上传id):"+jso);
	    String uploadID=jso.getJSONObject("payload").getString("uploadID");
	    System.out.println("uploadID:"+uploadID);
	    
	    
	    
	    int eachSize = Integer.valueOf(jsoReq.containsKey("size")?jsoReq.getInteger("size"):500000); 
	    boolean valid = true;
	    JSONArray fileInfos = new JSONArray();
	    if(eachSize==0){
	    	fileInfos.add(fileInfo);
	    }else{
	    	try{
		    	String name = fileInfo.getString("name");
		    	String ext = fileInfo.getString("ext");
		    	String root = filePath.substring(0, filePath.lastIndexOf(name));
		    	String prefix ="";
		    	if("".equals(ext))
		    	{
		    		prefix=name;
		    		
		    	}
		    	else
		    	{
		    		prefix = name.substring(0,name.lastIndexOf("."+ext));
		    	}
		    	
				String joint = "_";
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
				byte[] bytes = new byte[eachSize];
				int length = -1;
				int idx = 1;
				while((length =bis.read(bytes))>-1){ 
					JSONObject info = new JSONObject(); 
					String fpath =root+prefix+joint+idx+"."+ext;
					info.put("path", fpath);
					info.put("ext", ext);		
					if("".equals(ext))
					{
						info.put("name", prefix+joint+idx);
					}
					else
					{
						info.put("name", prefix+joint+idx+"."+ext);
					}
					
					
					try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fpath))){
						bos.write(bytes,0,length);
						idx++;
						info.put("size", length);
					}catch (Exception e) {
						e.printStackTrace();
						valid=false;
						break;
					}		
					info.put("crc", ToolsUtil.getFileCRCCode(new File(fpath)));
					fileInfos.add(info);
				}
				bis.close();
		    	if(!valid){
		    		jsoResp.put("respCode", "-1");
		    		jsoResp.put("respDesc", "jso-uploads 分片拆分("+prefix+joint+idx+")"+" 失败");
		    		jsoResp.put("respMsg", "jso-uploads 分片拆分("+prefix+joint+idx+")"+" 失败");
		    		return jsoResp;
		    	}	
	    	
	    	}catch(Exception e){
	    		jsoResp.put("respCode", "-1");
	    		jsoResp.put("respDesc", "jso-uploads 分片拆分异常");
	    		jsoResp.put("respMsg", "jso-uploads 分片拆分-开关流失败");
	    		return jsoResp;
	    	}	    	
	    }	    
	    
	    content ="";
	    for(int i=0;i<fileInfos.size();i++){
	    	int partNumber =i+1;
	    	JSONObject fi = fileInfos.getJSONObject(i);
	    	url = "https://"+tos_host+"/"+oid+"?partNumber="+partNumber+"&uploadID="+uploadID;		
	    	byte[] stream;	
		    stream=ToolsUtil.fileConvertToByteArray(new File(fi.getString("path")));
		    JSONObject paraData = new JSONObject();
	    	paraData.put("url", url);
	    	paraData.put("method", "PUT");
	    	paraData.put("stream", stream);			   
		    headers = new HashMap<String, String>();
			headers.put("Accept", "*/*"); 
			headers.put("Content-Type", "application/octet-stream");
			headers.put("authorization", tos_sign); 			
			headers.put("Host", tos_host);		 
			headers.put("Content-CRC32", fi.getString("crc"));			
			headers.put("Content-Length", stream.length+"");
	    	jso = ToolsUtil.uploadStream(paraData,headers);
	    	if(!jso.getString("respCode").equals("0")){
	    		valid = false;
	    		jsoResp.put("respCode", "-1");
	    		jsoResp.put("respDesc", "jso-uploads 分片上传("+partNumber+")_"+uploadID+"失败");
	    		jsoResp.put("respMsg", jso.getString("respDesc"));
	    		break;
	    	}
	    	content+= partNumber+":"+fi.getString("crc");
	    	if(i!=fileInfos.size()-1){
	    		content+=",";
	    	}
		    System.out.println("jso-uploads(分片上传("+partNumber+")_"+uploadID+"):"+jso);	    	
	    }
	    
	    if(!valid){
	    	return jsoResp;
	    }
	    
	    
	    
	    //循环分片
	    
	    
//	    int partNumber=1;		
//	    
//	    url = "https://"+tos_host+"/"+oid+"?partNumber="+partNumber+"&uploadID="+uploadID;			  
//	    filePath="D:\\video\\test.mp4";
//	    File file =new File(filePath);
//	    String fileCrc32= ToolsUtil.getFileCRCCode(new File(filePath));
//	    byte[] stream;	
//	    stream=ToolsUtil.fileConvertToByteArray(file);		 
//	    JSONObject paraData = new JSONObject();
//    	paraData.put("url", url);
//    	paraData.put("method", "PUT");
//    	paraData.put("stream", stream);			   
//	    headers = new HashMap<String, String>();
//		headers.put("Accept", "*/*"); 
//		headers.put("Content-Type", "application/octet-stream");
//		headers.put("authorization", tos_sign); 			
//		headers.put("Host", tos_host);		 
//		headers.put("Content-CRC32", fileCrc32);			
//		headers.put("Content-Length", stream.length+"");
//    	jso = ToolsUtil.uploadStream(paraData,headers);
//    	if(!jso.getString("respCode").equals("0")){
//    		jsoResp.put("respCode", "-1");
//    		jsoResp.put("respDesc", "jso-uploads 分片上传("+partNumber+")_"+uploadID+"失败");
//    		jsoResp.put("respMsg", jso.getString("respDesc"));
//    		return jsoResp;
//    	}
//	    System.out.println("jso-uploads(分片上传("+partNumber+")_"+uploadID+"):"+jso);
//	    
	    
	    
	    //组合分片
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "text/plain;charset=UTF-8");
		headers.put("authorization", tos_sign); 			
		headers.put("Host", tos_host);		 
		url = "https://"+tos_host+"/"+oid+"?&uploadID="+uploadID;	
		//1:f82ffeb5,2:eaabdcf1
	    jso =ToolsUtil.postHttps(url,content,headers) ;
	    if(jso.getInteger("success")!=0){
	    	jsoResp.put("respCode", "-1");
	    	jsoResp.put("respDesc", "jso-uploads 文件上传确认_"+uploadID+" 失败");
	    	jsoResp.put("respMsg", jso.toString());
	    	return jsoResp;
	    }
	    System.out.println("jso-uploads(文件上传确认_"+uploadID+"):"+jso);
	    
        
	    
	    url="https://vas-lf-x.snssdk.com/video/openapi/v1/?action=UpdateVideoUploadInfos&vidc=lf&vts=1586420571714421877&host="+tos_host+"&region=CN&province=12&edge_node=lf&upload_mode=serial&file_size="+fileSize+".000000&strategy=long_memory_filter_v2&user_ip=60.29.142.82";
	    headers = new HashMap<String, String>();		  
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", "application/json");
		headers.put("authorization", authorization); 	
		headers.put("x-tt-access", x_tt_access); 			
		JSONObject paraData = new JSONObject();
    	paraData.put("vid", vid);
    	paraData.put("oid", oid);
    	paraData.put("token", token);		
    	paraData.put("poster_ss", 0);	
    	paraData.put("is_exact_poster", false);	
    	paraData.put("user_reference", "");			 
		jso =ToolsUtil.postHttps(url,paraData.toString(),headers) ;
		if(jso.getInteger("code")!=2000){
			jsoResp.put("respCode", "-1");
			jsoResp.put("respDesc", "jso-openapi/v1-UpdateVideoUploadInfos()封面请求预准备 失败");
			jsoResp.put("respMsg", jso.toString());
			return jsoResp;
		}
		else
		{
			if(jso.containsKey("data"))
			{
				 vid=jso.getJSONObject("data").getString("vid");
				 oid=jso.getJSONObject("data").getJSONObject("poster").getString("oid");
					
			}
			
			jsoResp.put("vid", vid);
			jsoResp.put("oid", oid);
			jsoResp.put("respCode", "0");
			jsoResp.put("respDesc", "jso-openapi/v1-UpdateVideoUploadInfos()封面请求预准备 成功");
			jsoResp.put("respMsg", jso.toString());
			
		}
		
	 
		 
		
		return jsoResp;	
       
	}
	
	
	
	
	/**
	 * 抖音封面上传
	 * jsoReq: {"filePath":"","videoCoverCount":11,"size":500000}
	 */
	public  static JSONObject  uploadImageFileForDY(String cookies,JSONObject jsoReq)
	{
		
		JSONObject jsoResp= new JSONObject();
		JSONObject fileInfo = new JSONObject();   // {"name":"","path":"",ext:"","size":"","crc":""}
	    String filePath=jsoReq.getString("filePath");
	    File file = new File(filePath);
		if(file.exists()&&file.isFile()){
			try{
				String name = file.getName();
				fileInfo.put("name", name);
				fileInfo.put("path", filePath);
				fileInfo.put("ext", name.indexOf(".")>-1?name.split("\\.")[1]:"");
				fileInfo.put("size", String.valueOf(file.length()));				
				fileInfo.put("crc", ToolsUtil.getFileCRCCode(file));
				
			}catch(Exception e){
				e.printStackTrace();
				jsoResp.put("respCode","-1");
				jsoResp.put("respDesc", "获取"+filePath+" 文件信息异常");
				jsoResp.put("respMsg", "获取"+filePath+" 文件信息异常");
				return jsoResp;
			}
		}else{
			jsoResp.put("respCode","-1");
			jsoResp.put("respDesc", filePath+" 文件不存在");
			jsoResp.put("respMsg", filePath+" 文件不存在");
			return jsoResp;
		}
	    
	  
	    String authorization="";
	    String x_tt_access="";	  
	    String fileSize=fileInfo.getString("size"); // "4925317"
	    
	  //  String cookies="passport_csrf_token=6921d8ce370295d4f9d68bfff840e202;s_v_web_id=verify_k8qoaovq_f164B7VF_YiqQ_4WIN_BH8Z_07Dsfga5wKNL;sso_auth_status=c73d552366efd68ef87dfcfb2b7d51e9;sso_uid_tt=998a1307e1ec8ca7329b0e53f8ef3180;sso_uid_tt_ss=998a1307e1ec8ca7329b0e53f8ef3180;toutiao_sso_user=6aba4914ccd86eb6595ff0550de8a8f2;toutiao_sso_user_ss=6aba4914ccd86eb6595ff0550de8a8f2;passport_auth_status=74cd2aa025ce22a57f0e35605f4958d8%2C219ae1dfd2ee07ed455cd0752c9616a4;sid_guard=9bef42d870d744aa76169b5dedeb45ed%7C1586310501%7C5184000%7CSun%2C+07-Jun-2020+01%3A48%3A21+GMT;uid_tt=8f71874e3f3ca9a188fc86537f89b8c9;uid_tt_ss=8f71874e3f3ca9a188fc86537f89b8c9;sid_tt=9bef42d870d744aa76169b5dedeb45ed;sessionid=9bef42d870d744aa76169b5dedeb45ed;sessionid_ss=9bef42d870d744aa76169b5dedeb45ed;gftoken=OWJlZjQyZDg3MHwxNTg2MzEwNTAxNjh8fDAGBgYGBgY;SLARDAR_WEB_ID=9bd57122-94ce-4497-9675-52f309eef873;tt_webid=6813151764553516552";	
	 //	cookies="_ga=GA1.2.24301998.1584663458; install_id=0; sid_tt=d0a9a08842fb7d25b12fbea597240f86; sid_guard=d0a9a08842fb7d25b12fbea597240f86; sessionid=d0a9a08842fb7d25b12fbea597240f86; tt_webid=6810540743864059400; uid_tt=7db44c10f675780eb908c389f7c043e22dd149dff269cd4582a57c9ad6ee0e68; gftoken=ZDBhOWEwODg0MnwxNTg1NzA2NjcyNjN8fDAGBgYGBgY; SLARDAR_WEB_ID=1c7e18ec-ad55-4b57-9803-3e32fc17c539; passport_csrf_token=c4c13338cb91b256b557a5899a636b2e; sso_uid_tt=44388a0206e4f50fcab45d3ed3b0ea75; sso_uid_tt_ss=44388a0206e4f50fcab45d3ed3b0ea75; toutiao_sso_user=c95f7b9c8e7cf2008f7e914a0d3f6756; toutiao_sso_user_ss=c95f7b9c8e7cf2008f7e914a0d3f6756; s_v_web_id=k8r1steo_hNzQboyc_ASbU_4RuG_AGAp_aY9Bsqq6v3Zy";	    
	    String url = "https://media.douyin.com/web/api/media/upload/auth/";						
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("referer", "https://creator.douyin.com/content/");   
		headers.put("Host", "creator.douyin.com");
		headers.put("Origin", "creator.douyin.com");
		headers.put("Cookie", cookies);	 			
        JSONObject jso =ToolsUtil.getHttps(url,headers) ;
        System.out.println("jso-upload_token(获取上传空间服务器密钥):"+jso);	 
        if(jso.getInteger("status_code")!=0){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-upload_token 获取上传空间服务器密钥失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        
        authorization=jso.getString("auth");
        try
        {
        	x_tt_access=authorization.split(":")[3];
        }
        catch(Exception e)
        {
        	x_tt_access="56649dadda5ee92f4a264bb3ed2ea908";
        	e.printStackTrace();
        }
	    
	    
        headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/json");
		headers.put("authorization", authorization); 	
		headers.put("x-tt-access", x_tt_access); 			
        url = "https://vas-lf-x.snssdk.com/video/openapi/v1/?action=GetImageUploadParams&s=6m6u6iw2r04&use_edge_node=1&file_size="+fileSize;						
        jso =ToolsUtil.getHttps(url,headers) ;
        System.out.println("jso-openapi/v1(获取上传空间服务器许可证):"+jso);
        if(jso.getInteger("code")!=2000){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-openapi/v1 获取上传空间服务器许可证失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        String oid=jso.getJSONObject("data").getJSONArray("object_ids").getString(0);
        String tos_sign=jso.getJSONObject("data").getString("tos_sign");
        String tos_host=jso.getJSONObject("data").getString("tos_host");
       // String vid=jso.getJSONObject("data").getString("vid");
       // String token=jso.getJSONObject("data").getString("token");
       // System.out.println("vid----"+vid);
        //System.out.println("token----"+token);


        headers = new HashMap<String, String>();
	    url = "https://"+tos_host+"/"+oid;
	    
	    
	    byte[] stream;	
	    stream=ToolsUtil.fileConvertToByteArray(new File(fileInfo.getString("path")));
	    JSONObject paraData = new JSONObject();
    	paraData.put("url", url);
    	paraData.put("method", "PUT");
    	paraData.put("stream", stream);			   
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "application/octet-stream");
		headers.put("authorization", tos_sign); 	
		headers.put("Referer", "https://creator.douyin.com/content/publish/poster/"); 	
		
		headers.put("Host", tos_host);		 
		headers.put("Content-CRC32", fileInfo.getString("crc"));			
		headers.put("Content-Length", stream.length+"");
		jsoResp = ToolsUtil.uploadStream(paraData,headers);
	    
	    
	    
	    
	    if(jso.getInteger("code")!=2000){
	    	jsoResp.put("respCode", "-1");
	    	jsoResp.put("respDesc", "jso-uploads 文件上传失败");
	    	jsoResp.put("respMsg", jso.toString());
	    	return jsoResp;
	    }
	    else
	    {
	    		
	    	jsoResp.put("oid", oid);
	    	jsoResp.put("respCode", "0");
	    	jsoResp.put("respDesc", "jso-uploads 文件上传成功");
	    	jsoResp.put("respMsg", jso.toString());
	    }
	  
		
		return jsoResp;	
       
	}
	
	
    /**
     * uid
     * filePath
     * cookies
     * text
     * jsPath
     */
	public  static JSONObject sendVideoForDY(JSONObject reqData)			
	{	
	 	String  uid=!reqData.containsKey("uid") ? "" : reqData.getString("uid");		
	 	String  cookies=!reqData.containsKey("cookies") ? "" : reqData.getString("cookies");	
	 	String  filePath=!reqData.containsKey("filePath") ? "" : reqData.getString("filePath");	
		String  text=!reqData.containsKey("text") ? "" : reqData.getString("text");	
		String  jsPath=!reqData.containsKey("jsPath") ? "" : reqData.getString("jsPath");	
		String  imgfilePath=!reqData.containsKey("imgfilePath") ? "" : reqData.getString("imgfilePath");
//		jsPath="D:\\signature.js";
		String objectStr="dycs";
		String func="sign";		
		String[] params = new String[]{uid};
//		Object _signature=ToolsUtil.invokeJS(jsPath, objectStr,func, params);
		Object _signature= 123456789;
		System.out.println(_signature);
	   // cookies="_ga=GA1.2.1597872624.1590045728; s_v_web_id=kaq2as4x_RLPMl0jT_1l1a_4X1d_9HvK_vcKCJ2t0B7Wo; csrf_token=qhftNNBGwTBvGXPgyOBEdCONOxkVssNS; ttwid=6831691329191216653; passport_csrf_token=3ac412f320eca2712647757c32f6c35c; odin_tt=4ac235c96752cb8fae57ea668710a8df132d308c1287d2841525d0f83458e3cbe3aabc58c9be4a039ce7700e46f8b9f3; sso_uid_tt=3259b030a0aa00a8649760384f5524e4; sso_uid_tt_ss=3259b030a0aa00a8649760384f5524e4; toutiao_sso_user=f6fb185d7ca2a25b38f59b9ce346802d; toutiao_sso_user_ss=f6fb185d7ca2a25b38f59b9ce346802d; passport_auth_status=a5f6e438aef0479cf4cf279c0fe081a2%2C; sid_guard=7c6d4822db1050e11402358d5ab35e14%7C1590627561%7C5184000%7CMon%2C+27-Jul-2020+00%3A59%3A21+GMT; uid_tt=16e5817e4913663e0b87b6fe83452aee; uid_tt_ss=16e5817e4913663e0b87b6fe83452aee; sid_tt=7c6d4822db1050e11402358d5ab35e14; sessionid=7c6d4822db1050e11402358d5ab35e14; sessionid_ss=7c6d4822db1050e11402358d5ab35e14; oc_login_type=LOGIN_PERSON; SLARDAR_WEB_ID=9229c99a-f302-45cf-ba15-e1563b4d2462";
	   String pattern = "csrf_token=(.*?);";
	   String value=ToolsUtil.easyMatch(cookies, pattern);		  
	   System.out.println(value);		  
	   JSONObject jsoReq = new JSONObject();
	   JSONObject jsoRsp = new JSONObject();
	   jsoReq.put("filePath", filePath);		  
	   jsoReq.put("size", "50000000");
	   jsoRsp=ProtocolUtil.uploadVideoFileForDY(cookies, jsoReq);
	   System.out.println(jsoRsp.toJSONString());
	   String vid="";
	   String oid="";			   
	    vid=jsoRsp.getString("vid");
	    oid=jsoRsp.getString("oid");
	    HashMap<String, String> headers = new HashMap<String, String>();
	//   obj="8-G8NAAgEAgf3ccGES9Ym.PhvCAAK05";
	    String  url = "https://creator.douyin.com/web/api/media/aweme/create/?cookie_enabled=true&screen_width=1360&screen_height=768&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F81.0.4044.92+Safari%2F537.36&browser_online=true&timezone_name=Asia%2FShanghai&aid=1128&_signature="+_signature;		
	   // String  url = "https://creator.douyin.com/web/api/media/sync/income/?cookie_enabled=true&screen_width=1360&screen_height=768&browser_language=zh-CN&browser_platform=Win32&browser_name=Mozilla&browser_version=5.0+(Windows+NT+10.0%3B+Win64%3B+x64)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F81.0.4044.92+Safari%2F537.36&browser_online=true&timezone_name=Asia%2FShanghai&aid=1128&_signature="+_signature;		

	    headers = new HashMap<String, String>();		  
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("referer", "https://creator.douyin.com/content/publish");   
		headers.put("Host", "creator.douyin.com");
		headers.put("Origin", "creator.douyin.com");
		headers.put("x-csrf-token", value);
		
		headers.put("Cookie", cookies);	 	
		
	//	text="这是我的第一个视频abc!";
		String oid1="";
		System.out.println("原封面ID("+oid+")");
		if(!"".equals(imgfilePath))
		{
			 JSONObject jsoReq1 = new JSONObject();
			 JSONObject jsoRsp1 = new JSONObject();	
			 jsoReq1.put("filePath", imgfilePath);		  
			 jsoRsp1=ProtocolUtil.uploadImageFileForDY(cookies,jsoReq1);
			 oid1=jsoRsp1.getString("oid");
			 if(!"".equals(oid1))
			 {
				 oid=oid1;
			 }
			 
		}
		System.out.println("新封面ID("+oid+")");
		
		//visibility_type 0 公开   1 自己可见
		String content="video_id="+vid+"&poster="+oid+"&poster_delay=0&cover_text_uri=&cover_text=&text="+text+"&text_extra=%5B%5D&challenges=%5B%5D&mentions=%5B%5D&visibility_type=0&third_text="+text+"&download=0&upload_source=1&mix_id=&mix_order=&is_preview=0&category=104";	      
	    JSONObject jsoResp =ToolsUtil.postHttps(url, content, headers) ;
        System.out.println("sendVideoForDy(上传):"+jsoResp);	
		
		
		
        if(0==jsoResp.getInteger("status_code"))	    {
        	System.out.println("sendVideoForDy(发送抖音视频)--成功:"+jsoResp);
	    	
	    	
	    }    
	    else
	    {
	    	System.out.println("sendVideoForDy(发送抖音视频)--失败:"+jsoResp);	    
	    	
	    }
	
        
	  
			    
	    return jsoResp;
	}
	
	
	/**
	 * 快手视频上传
	 * jsoReq: {"filePath":"","videoCoverCount":11,"size":500000}
	 */
	public  static JSONObject  uploadVideoFileForKS(String cookies,JSONObject jsoReq)
	{
		
		JSONObject jsoResp= new JSONObject();
		JSONObject fileInfo = new JSONObject();   // {"name":"","path":"",ext:"","size":"","crc":""}
	    String filePath=jsoReq.getString("filePath");
	    File file = new File(filePath);
		if(file.exists()&&file.isFile()){
			try{
				String name = file.getName();
				fileInfo.put("name", name);
				fileInfo.put("path", filePath);
				fileInfo.put("ext", name.indexOf(".")>-1?name.split("\\.")[1]:"");
				fileInfo.put("size", String.valueOf(file.length()));
				if(jsoReq.getInteger("size")==0){
					//fileInfo.put("crc", ToolsUtil.getFileCRCCode(file));
				}
			}catch(Exception e){
				e.printStackTrace();
				jsoResp.put("respCode","-1");
				jsoResp.put("respDesc", "获取"+filePath+" 文件信息异常");
				jsoResp.put("respMsg", "获取"+filePath+" 文件信息异常");
				return jsoResp;
			}
		}else{
			jsoResp.put("respCode","-1");
			jsoResp.put("respDesc", filePath+" 文件不存在");
			jsoResp.put("respMsg", filePath+" 文件不存在");
			return jsoResp;
		}
	    
	  
	    String authorization="";
	    String x_tt_access="";
	    String content="";
	    String fileSize=fileInfo.getString("size"); // "4925317"
	    
	    String url = "https://cp.kuaishou.com/rest/cp/works/upload/video/preupload";						
	    Map<String, String> headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/x-www-form-urlencoded;charset=UTF-8");
		headers.put("referer", "https://cp.kuaishou.com/article/publish/video");   
		headers.put("Host", "cp.kuaishou.com");
		headers.put("Origin", "https://cp.kuaishou.com");
		headers.put("Cookie", cookies);	 	
		JSONObject jsorq = new JSONObject();
		jsorq.put("kuaishou.web.cp.api_ph", jsoReq.getString("kuaishou.web.cp.api_ph"));
		
        JSONObject jso =ToolsUtil.postHttps(url, jsorq.toJSONString(), headers);
        System.out.println("jso-upload_token(获取上传空间服务器密钥):"+jso);	 
        if(jso.getInteger("result")!=1){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-upload_token 获取上传空间服务器密钥失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        
        authorization=jso.getJSONObject("data").getString("token");
        try
        {
        	x_tt_access="";
        }
        catch(Exception e)
        {
        	x_tt_access="";
        	e.printStackTrace();
        }
	    
	    

	    
	    
	    int eachSize = Integer.valueOf(jsoReq.containsKey("size")?jsoReq.getInteger("size"):500000); 
	    boolean valid = true;
	    JSONArray fileInfos = new JSONArray();
	    if(eachSize==0){
	    	fileInfos.add(fileInfo);
	    }else{
	    	try{
		    	String name = fileInfo.getString("name");
		    	String ext = fileInfo.getString("ext");
		    	String root = filePath.substring(0, filePath.lastIndexOf(name));
		    	String prefix ="";
		    	if("".equals(ext))
		    	{
		    		prefix=name;
		    		
		    	}
		    	else
		    	{
		    		prefix = name.substring(0,name.lastIndexOf("."+ext));
		    	}
				String joint = "_";
				BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
				byte[] bytes = new byte[eachSize];
				int length = -1;
				int idx = 1;
				while((length =bis.read(bytes))>-1){ 
					JSONObject info = new JSONObject(); 
					String fpath =root+prefix+joint+idx+"."+ext;
					info.put("path", fpath);
					info.put("ext", ext);
					if("".equals(ext))
					{
						info.put("name", prefix+joint+idx);
					}
					else
					{
						info.put("name", prefix+joint+idx+"."+ext);
					}
					try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fpath))){
						bos.write(bytes,0,length);
						idx++;
						info.put("size", length);
					}catch (Exception e) {
						e.printStackTrace();
						valid=false;
						break;
					}		
					//info.put("crc", ToolsUtil.getFileCRCCode(new File(fpath)));
					fileInfos.add(info);
				}
				bis.close();
		    	if(!valid){
		    		jsoResp.put("respCode", "-1");
		    		jsoResp.put("respDesc", "jso-uploads 分片拆分("+prefix+joint+idx+")"+" 失败");
		    		jsoResp.put("respMsg", "jso-uploads 分片拆分("+prefix+joint+idx+")"+" 失败");
		    		return jsoResp;
		    	}	
	    	
	    	}catch(Exception e){
	    		jsoResp.put("respCode", "-1");
	    		jsoResp.put("respDesc", "jso-uploads 分片拆分异常");
	    		jsoResp.put("respMsg", "jso-uploads 分片拆分-开关流失败");
	    		return jsoResp;
	    	}	    	
	    }	    
	    
	    content ="";
	    int partNumber=0;
	    for(int i=0;i<fileInfos.size();i++){
	    	partNumber =i+1;
	    	JSONObject fi = fileInfos.getJSONObject(i);
	    	url = "https://upload.kuaishouzt.com/api/upload/fragment?fragment_id="+i+"&upload_token="+authorization;		
	    	byte[] stream;	
		    stream=ToolsUtil.fileConvertToByteArray(new File(fi.getString("path")));
		    JSONObject paraData = new JSONObject();
	    	paraData.put("url", url);
	    	paraData.put("method", "POST");
	    	paraData.put("stream", stream);			   
		    headers = new HashMap<String, String>();
			headers.put("Accept", "*/*"); 
			headers.put("Content-Type", "application/octet-stream");				
			headers.put("Host", "upload.kuaishouzt.com");		
	    	jso = ToolsUtil.uploadStream(paraData,headers);
	    	if(!jso.getString("respCode").equals("0")){
	    		valid = false;
	    		jsoResp.put("respCode", "-1");
	    		jsoResp.put("respDesc", "jso-uploads 分片上传("+partNumber+")_"+"失败");
	    		jsoResp.put("respMsg", jso.getString("respDesc"));
	    		break;
	    	}
	  
		    System.out.println("jso-uploads(分片上传("+partNumber+"):"+jso);	    	
	    }
	    
	    if(!valid){
	    	return jsoResp;
	    }
	    
	    
	    
	
	    
	    //组合分片
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); 
		headers.put("Content-Type", "text/plain;charset=UTF-8");			
		headers.put("Host", "upload.kuaishouzt.com");		
		url = "https://upload.kuaishouzt.com/api/upload/complete?fragment_count="+partNumber+"&upload_token="+authorization;	
		//1:f82ffeb5,2:eaabdcf1
	    jso =ToolsUtil.postHttps(url,content,headers) ;
	    if(jso.getInteger("result")!=1){
	    	jsoResp.put("respCode", "-1");
	    	jsoResp.put("respDesc", "jso-uploads 文件上传确认_失败");
	    	jsoResp.put("respMsg", jso.toString());
	    	return jsoResp;
	    }
	    System.out.println("jso-uploads(文件上传确认):"+jso);
	    
        
	    String fileId="";
	    String coverKey="";
	    url = "https://cp.kuaishou.com/rest/cp/works/upload/video/finish";						
	    headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/json;charset=UTF-8");
		headers.put("referer", "https://cp.kuaishou.com/article/publish/video");   
		headers.put("Host", "cp.kuaishou.com");
		headers.put("Origin", "https://cp.kuaishou.com");
		headers.put("Cookie", cookies);	 	
		jsorq = new JSONObject();
		jsorq.put("fileLength",fileInfo.getString("size"));
		jsorq.put("fileName",fileInfo.getString("name"));
		jsorq.put("fileType","video/mp4");
		jsorq.put("kuaishou.web.cp.api_ph", jsoReq.getString("kuaishou.web.cp.api_ph"));
		jsorq.put("token", authorization);
        jso =ToolsUtil.postHttps(url, jsorq.toJSONString(), headers);
      
        if(jso.getInteger("result")!=1){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-upload_finish 视频文件生成失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        else
        {
        	jsoResp.put("respCode", "0");
        	jsoResp.put("respDesc", "jso-upload_finish 视频文件生成成功");
        	jsoResp.put("respMsg", jso.toString());
        	fileId=jso.getJSONObject("data").getString("fileId");
        	coverKey=jso.getJSONObject("data").getString("coverKey");       	
        }
	 
        if(!"".equals(jsoReq.getString("imgfilePath")))
        {
        	JSONObject jsoReq2 = new JSONObject();
        	JSONObject jsoResp2 = new JSONObject();
        	jsoReq2.put("filePath", jsoReq.getString("imgfilePath"));
        	jsoReq2.put("kuaishou.web.cp.api_ph", jsoReq.getString("kuaishou.web.cp.api_ph"));
        	jsoResp2=ProtocolUtil.uploadImageFileForKS(cookies, jsoReq2);
        	System.out.println("uploadImageFileForKS-封面获取"+jsoResp2.toJSONString());
        	if("1".equals(jsoResp2.getJSONObject("result").getString("result")))
        	 {
        		coverKey=jsoResp2.getJSONObject("result").getJSONObject("data").getString("coverKey");   
        	}
        }
        
        url = "https://cp.kuaishou.com/rest/pc/photo/submit";						
	    headers = new HashMap<String, String>();
	    headers = new HashMap<String, String>();
		headers.put("Accept", "*/*"); //*/*
		headers.put("Content-Type", " application/json;charset=UTF-8");
		headers.put("referer", "https://cp.kuaishou.com/article/publish/video");   
		headers.put("Host", "cp.kuaishou.com");
		headers.put("Origin", "https://cp.kuaishou.com");
		headers.put("Cookie", cookies);	 	
		jsorq = new JSONObject();
		jsorq.put("caption",jsoReq.getString("text"));
		jsorq.put("coverKey",coverKey);
		jsorq.put("coverTimeStamp",-1);
		jsorq.put("coverTitle",jsoReq.getString("title"));
		jsorq.put("coverTitleStyle","");
		jsorq.put("coverTitleXCoordinate","");
		jsorq.put("coverTitleYCoordinate","");
		jsorq.put("coverType",1);
		jsorq.put("fileId",fileId);
		jsorq.put("kuaishou.web.cp.api_ph", jsoReq.getString("kuaishou.web.cp.api_ph"));
		jsorq.put("photoStatus",1);
		jsorq.put("photoType",0);
		jsorq.put("publishTime",0);	
	
        jso =ToolsUtil.postHttps(url, jsorq.toJSONString(), headers);
      
        if(jso.getInteger("result")!=1){
        	jsoResp.put("respCode", "-1");
        	jsoResp.put("respDesc", "jso-upload_submit 视频发布失败");
        	jsoResp.put("respMsg", jso.toString());
        	return jsoResp;
        }
        else
        {
        	jsoResp.put("respCode", "0");
        	jsoResp.put("respDesc", "jso-upload_submit 视频发布成功");
        	jsoResp.put("respMsg", jso.toString());
        	      	
        }

		
		return jsoResp;	
       
	}
	
	
	
	/**
	 * 快手封面上传
	 * jsoReq: {"filePath":"","videoCoverCount":11,"size":500000}
	 */
	public  static JSONObject  uploadImageFileForKS(String cookies,JSONObject jsoReq)
	{
		 Map<String, String> headers = new HashMap<String, String>();
		JSONObject jsoResp= new JSONObject();
	    String filePath=jsoReq.getString("filePath");
	    String value=jsoReq.getString("kuaishou.web.cp.api_ph");		    
	    Map<String, String> textMap = new HashMap<String, String>();
	    //可以设置多个input的name，value
	    textMap.put("kuaishou.web.cp.api_ph", value);    
        //设置file的name，路径
        Map<String, String> fileMap = new HashMap<String, String>();
        fileMap.put("file", filePath);
        String fileType = "";//image/png
	    headers = new HashMap<String, String>();				
		headers.put("Accept", "*/*"); 	
		headers.put("Cookie", cookies); 	
		headers.put("Host", "cp.kuaishou.com"); 	
		headers.put("Origin", "https://cp.kuaishou.com"); 
		headers.put("Referer", "https://cp.kuaishou.com/article/publish/video"); 
		JSONObject paraData= new JSONObject();
		String url="https://cp.kuaishou.com/rest/cp/works/upload/video/cover/upload";
		paraData.put("url", url);
    	paraData.put("method", "POST");		    	 
		jsoResp = ToolsUtil.uploadStream(paraData,headers,textMap,fileMap,fileType);	
		
		return jsoResp;	
       
	}
	
	/**
     * uid
     * filePath
     * cookies
     * text
     * jsPath
     */
	public  static JSONObject sendVideoForKS(JSONObject reqData)			
	{	
	 	String  uid=!reqData.containsKey("uid") ? "" : reqData.getString("uid");		
	 	String  cookies=!reqData.containsKey("cookies") ? "" : reqData.getString("cookies");	
	 	String  filePath=!reqData.containsKey("filePath") ? "" : reqData.getString("filePath");	
	 	String  title=!reqData.containsKey("title") ? "" : reqData.getString("title");	
	 	String  text=!reqData.containsKey("text") ? "" : reqData.getString("text");	
		String  jsPath=!reqData.containsKey("jsPath") ? "" : reqData.getString("jsPath");	
		String  imgfilePath=!reqData.containsKey("imgfilePath") ? "" : reqData.getString("imgfilePath");
//		jsPath="D:\\signature.js";
		String objectStr="dycs";
		String func="sign";		
		String  [] params = new String[]{uid};
		//Object _signature=ToolsUtil.invokeJS(jsPath, objectStr,func, params);
		//System.out.println(_signature);
	   // cookies="_ga=GA1.2.1597872624.1590045728; s_v_web_id=kaq2as4x_RLPMl0jT_1l1a_4X1d_9HvK_vcKCJ2t0B7Wo; csrf_token=qhftNNBGwTBvGXPgyOBEdCONOxkVssNS; ttwid=6831691329191216653; passport_csrf_token=3ac412f320eca2712647757c32f6c35c; odin_tt=4ac235c96752cb8fae57ea668710a8df132d308c1287d2841525d0f83458e3cbe3aabc58c9be4a039ce7700e46f8b9f3; sso_uid_tt=3259b030a0aa00a8649760384f5524e4; sso_uid_tt_ss=3259b030a0aa00a8649760384f5524e4; toutiao_sso_user=f6fb185d7ca2a25b38f59b9ce346802d; toutiao_sso_user_ss=f6fb185d7ca2a25b38f59b9ce346802d; passport_auth_status=a5f6e438aef0479cf4cf279c0fe081a2%2C; sid_guard=7c6d4822db1050e11402358d5ab35e14%7C1590627561%7C5184000%7CMon%2C+27-Jul-2020+00%3A59%3A21+GMT; uid_tt=16e5817e4913663e0b87b6fe83452aee; uid_tt_ss=16e5817e4913663e0b87b6fe83452aee; sid_tt=7c6d4822db1050e11402358d5ab35e14; sessionid=7c6d4822db1050e11402358d5ab35e14; sessionid_ss=7c6d4822db1050e11402358d5ab35e14; oc_login_type=LOGIN_PERSON; SLARDAR_WEB_ID=9229c99a-f302-45cf-ba15-e1563b4d2462";
	   String pattern = "kuaishou.web.cp.api_ph=(.*?);";
	   String value=ToolsUtil.easyMatch(cookies, pattern);		  
	   System.out.println(value);		  
	   JSONObject jsoReq = new JSONObject();
	   JSONObject jsoRsp = new JSONObject();
	   jsoReq.put("filePath", filePath);		  
	   jsoReq.put("size", "50000000");
	   jsoReq.put("kuaishou.web.cp.api_ph", value);
	   jsoReq.put("title", title);
	   jsoReq.put("text", text);
	   jsoReq.put("imgfilePath", imgfilePath);
	   jsoRsp=ProtocolUtil.uploadVideoFileForKS(cookies, jsoReq);
	   System.out.println(jsoRsp.toJSONString());		    
	   return jsoRsp;
	}
	
	
	/**
	 * 提取抖音视频地址
	 * @param reqData
	 * @return
	 */
	public  static JSONObject fetchViedoUrlForDY (JSONObject reqData)			
	{	    
		    JSONObject jso =new JSONObject();
		    String  url=!reqData.containsKey("url") ? "" : reqData.getString("url");	  
		    Map<String, String> headers = new HashMap<String, String>();
		    headers = new HashMap<String, String>();
			headers.put("Accept", "*/*"); //*/*				
	        JSONObject jsoRsp =ToolsUtil.getHttps(url,headers) ;	
	        String  location=!jsoRsp.containsKey("location") ? "" : jsoRsp.getString("location");	  
	        System.out.println("location:"+location);
//	        String pattern = "csrf_token=(.*?);";
	        String pattern = "video/(.*?)[/][?]region";
	 	    String value=ToolsUtil.easyMatch(location, pattern);		  
	 	   
	 	    url="https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids="+value;	 	    	
	 	    headers = new HashMap<String, String>();
			headers.put("Accept", "*/*"); //*/*				
	        jsoRsp =ToolsUtil.getHttps(url,headers) ;    
	        if(jsoRsp.getString("status_code").equals("0")){
	        	String desc=jsoRsp.getJSONArray("item_list").getJSONObject(0).getString("desc");
	        	String videoUrl=jsoRsp.getJSONArray("item_list").getJSONObject(0).getJSONObject("video").getJSONObject("play_addr").getJSONArray("url_list").getString(0);
	        	String coverUrl=jsoRsp.getJSONArray("item_list").getJSONObject(0).getJSONObject("video").getJSONObject("origin_cover").getJSONArray("url_list").getString(0);

	        	jso.put("title", desc);
	        	jso.put("video", videoUrl);
	        	jso.put("cover", coverUrl);
	        	jso.put("respCode", "0");
	    	}
	        else
	        {
	        	jso.put("respCode", "-1");
	        	
	        }
	        jso.put("result", jsoRsp);
	        return jso;
	}
}
